#include "stdafx.h"
#ifdef _DEBUG
	#undef THIS_FILE
	static char THIS_FILE[] = __FILE__;
	#define new DEBUG_NEW
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////
UWPButtonList::UWPButtonList(void):_clrText(RGB(255,255,255)),_wndControl(NULL)
{
	UWPButtonList::Reset();
}

UWPButtonList::~UWPButtonList(void)
{
}

void UWPButtonList::Reset(void)
{
	memset(_showFlag,0,sizeof(_showFlag));
	memset(rtList,0,sizeof(rtList));
	memset(szText,0,sizeof(szText));
	memset(uaLenText,0,sizeof(uaLenText));
	memset(aIcon,0,sizeof(aIcon));
	memset(uaType,0,sizeof(uaType));
	memset(uaMessage,0,sizeof(uaMessage));
	memset(uwParam,0,sizeof(uwParam));
	memset(ulParam,0,sizeof(ulParam));
	memset(puaMenu,0,sizeof(CMenu*)*__max_btnlist__);
	__current__ = -1;
	__numlist__ = __numshow__ = 0;
}

void UWPButtonList::GetMenuInfo(HMENU hMenu)
{
	memset(szText,0,sizeof(szText));
	memset(uaLenText,0,sizeof(uaLenText));
	memset(aIcon,0,sizeof(aIcon));
	__numlist__=GetMenuItemCount(hMenu);
	__numlist__=min(__numlist__,__max_btnlist__);
	for(long i=0;i<__numlist__;++i)
	{
		GetMenuString(hMenu,i,szText[i],256,MF_BYPOSITION);
		uaLenText[i]=lstrlen(szText[i]);
	}
}

long UWPButtonList::DrawMenu(HDC hDC,long lseek,long width,long lTop,long lBottom)
{
	long i(0);
	SIZE sz={0};
	if(__numlist__) lseek+=__MENU_EDGE__;
	for(;i<__numlist__&&lseek+100<width;++i)
	{
		GetTextExtentPoint32(hDC,szText[i],uaLenText[i],&sz);
		SetRect(&rtList[i],lseek,lTop,lseek+sz.cx+__MENU_EDGE__*2,lBottom);
		if(aIcon[i]) rtList[i].right += 22;
		SetTextColor(hDC,__menu_clr_text__);
		if(__current__==i)
		{
			uwpCopyBitmapMask(hDC,rtList[i].left,rtList[i].top,
				rtList[i].right-rtList[i].left,rtList[i].bottom-rtList[i].top,
				2,2,2,2,menu_frame_bitmap[0],0,16,23,__system_clrmask__,TRUE);
		}
		if(aIcon[i])
		{
			DrawIconEx(hDC,rtList[i].left+4,lTop+2,aIcon[i],16,16,0,0,DI_NORMAL);
			rtList[i].left += 22;
			DrawText(hDC,szText[i],uaLenText[i],&rtList[i],DT_LEFT|DT_VCENTER|DT_SINGLELINE);
			rtList[i].left -= 22;
		}
		else
		{
			DrawText(hDC,szText[i],uaLenText[i],&rtList[i],DT_CENTER|DT_VCENTER|DT_SINGLELINE);
		}
		lseek += (rtList[i].right-rtList[i].left);
	}
	__numshow__ = i;
	return(lseek);
}

BOOL UWPButtonList::PtInList(POINT&pt)
{
	long last=__current__,i=0;
	for(__current__=-1;i<__numshow__;++i)
	{
		if(_showFlag[i]) continue;
		if(PtInRect(&rtList[i],pt))
		{
			__current__ = i;
			::SetCursor(__cursor_hand__);
			break;
		}
	}
	return(__current__!=last);
}

long UWPButtonList::CheckNowID(POINT&pt)
{
	long i=(__numshow__-1);
	for(;i>-1;--i)
	{
		if(!_showFlag[i]&&PtInRect(&rtList[i],pt)) break;
	}
	return(i);
}

void UWPButtonList::Insert(UINT uType,LPCTSTR lpText,HICON hIcon,
						   UINT uMsg,WPARAM wParam,LPARAM lParam,CMenu*pMenu)
{
	if(__numlist__<__max_btnlist__)
	{
		uaType[__numlist__] = uType;
		lstrcpyn(szText[__numlist__],lpText,256);
		uaLenText[__numlist__] = lstrlen(lpText);
		aIcon[__numlist__] = hIcon;
		uaMessage[__numlist__] = uMsg;
		uwParam[__numlist__] = wParam;
		ulParam[__numlist__] = lParam;
		puaMenu[__numlist__] = pMenu;
		++__numlist__;
	}
}

void UWPButtonList::SetItem(long lIndex,LPCTSTR lpText,HICON hIcon,
						   UINT uMsg,WPARAM wParam,LPARAM lParam,CMenu*pMenu)
{
	if(lIndex<__numlist__)
	{
		lstrcpyn(szText[lIndex],lpText,256);
		uaLenText[lIndex] = lstrlen(lpText);
		aIcon[lIndex] = hIcon;
		uaMessage[lIndex] = uMsg;
		uwParam[lIndex] = wParam;
		ulParam[lIndex] = lParam;
		puaMenu[lIndex] = pMenu;
	}
}

void UWPButtonList::ShowButton(long lIndex,BOOL blShow)
{
	if(lIndex<__numlist__)
	{
		_showFlag[lIndex] = blShow?0:1;
	}
}

void UWPButtonList::ResetPosition(long lLeft,long lTop,long lRight,long lBottom)
{
	long i(0),maxWidth(80),lOffset(lLeft);
	if(__numlist__>0)
	{
		if(lRight-lLeft*2<maxWidth*__numlist__)
			maxWidth = (lRight-lLeft*2)/__numlist__;
	}
	for(;i<__numlist__;++i)
	{
		switch(uaType[i])
		{
		case	__shortcut_button__:
		case	__shortcut_combox__:
		case	__shortcut_combox2__:
			SetRect(&rtList[i],lOffset,lTop,lOffset+maxWidth-3,lBottom);
			lOffset += maxWidth;
			break;
		case	__shortcut_iconbutton__:
			SetRect(&rtList[i],lOffset,lTop,lOffset+16,lBottom);
			lOffset += 20;
			break;
		}
	}
	__numshow__ = i;
}

void UWPButtonList::ResetPosition2(long lLeft,long lTop,long lRight,long lBottom)
{
	long i(__numlist__-1),lOffset(lRight),maxWidth(80);
	for(;i>=0;--i)
	{
		switch(uaType[i])
		{
		case	__shortcut_button__:
		case	__shortcut_combox__:
		case	__shortcut_combox2__:
			SetRect(&rtList[i],lOffset-maxWidth+3,lTop,lOffset,lBottom);
			lOffset -= maxWidth;
			break;
		case	__shortcut_iconbutton__:
			SetRect(&rtList[i],lOffset-16,lTop,lOffset,lBottom);
			lOffset -= 20;
			break;
		}
	}
	__numshow__ = __numlist__;
}

void UWPButtonList::DrawList(HDC hDC)
{
	long i(0);
	for(;i<__numlist__;++i)
	{
		if(_showFlag[i]) continue;
		switch(uaType[i])
		{
		case	__shortcut_button__:
			DrawButton(hDC,&rtList[i],aIcon[i],szText[i],uaLenText[i],i==__current__,FALSE,FALSE);
			break;
		case	__shortcut_combox__:
			DrawButton(hDC,&rtList[i],aIcon[i],szText[i],uaLenText[i],i==__current__,TRUE,FALSE);
			break;
		case	__shortcut_combox2__:
			DrawButton(hDC,&rtList[i],aIcon[i],szText[i],uaLenText[i],i==__current__,TRUE,TRUE);
			break;
		case	__shortcut_iconbutton__:
		case	__shortcut_iconbutton2__:
			if(i==__current__)
				DrawIconEx(hDC,rtList[i].left-1,rtList[i].top-2,aIcon[i],18,18,0,0,DI_NORMAL);
			else
				DrawIconEx(hDC,rtList[i].left,rtList[i].top,aIcon[i],16,16,0,0,DI_NORMAL);
			break;
		}
	}
}

void UWPButtonList::DrawButton(HDC hDC,LPRECT lpRect,HICON hIcon,
							   LPCTSTR lpText,long lenText,
							   long lnFocus,BOOL blCombox,BOOL blCombox2)
{
	RECT rtDraw=*lpRect;
	uwpCopyBitmapMask(hDC,lpRect->left,lpRect->top,
		lpRect->right-lpRect->left,lpRect->bottom-lpRect->top,
		2,2,2,2,toolbar_bitmap_item[lnFocus],0,16,23,__system_clrmask__,TRUE);
	if(blCombox)
	{
		rtDraw.right -= 22;
		if(blCombox2&&lnFocus)
		{
			HGDIOBJ oldpen=SelectObject(hDC,toolbar_pen_border);
			MoveToEx(hDC,rtDraw.right,rtDraw.top,NULL);
			LineTo(hDC,rtDraw.right,rtDraw.bottom);
			SelectObject(hDC,oldpen);
		}
		HDC dcMem=CreateCompatibleDC(hDC);
		HGDIOBJ oldBitmap=SelectObject(dcMem,toolbar_bitmap_arrow[lnFocus]);
		TransparentBlt(hDC,rtDraw.right+6,rtDraw.top+9,
			9,5,dcMem,0,0,9,5,__system_clrmask__);
		SelectObject(dcMem,oldBitmap);
		DeleteDC(dcMem);
		rtDraw.right += (blCombox2&&lnFocus)?4:10;
	}
	SetTextColor(hDC,toolbar_clrtext[lnFocus]);
	if(hIcon)
	{
		DrawIconEx(hDC,rtDraw.left+2,(rtDraw.top+rtDraw.bottom-16)/2,hIcon,16,16,0,0,DI_NORMAL);
		rtDraw.left += 20;
	}
	DrawText(hDC,lpText,lenText,&rtDraw,DT_SINGLELINE|DT_VCENTER|DT_CENTER|DT_END_ELLIPSIS);
}

void UWPButtonList::OnLButtonDown(POINT&point)
{
	if(__current__<0||__current__>=__numlist__) return;
	if(!_wndControl)
		_wndControl=__wnd_mainframe__;
	if(_showFlag[__current__]) return;
	switch(uaType[__current__])
	{
	case	__shortcut_button__:
	case	__shortcut_iconbutton__:
	case	__shortcut_iconbutton2__:
		if(uaMessage[__current__])
			::PostMessage(_wndControl,uaMessage[__current__],uwParam[__current__],ulParam[__current__]);
		break;
	case	__shortcut_combox__:
		if(puaMenu[__current__])
		{
			UWPMenu9 menu;
			POINT pt={rtList[__current__].left-5,-3};
			::ClientToScreen(_wndControl,&pt);
			menu.MapMenuFrom(puaMenu[__current__]);
			__in_menu_pop__ = TRUE;
			menu.TrackPopMenu(pt.x,pt.y,_wndControl,TPM_RIGHTALIGN);
			__in_menu_pop__ = FALSE;
		}
		break;
	case	__shortcut_combox2__:
		if(point.x+28>rtList[__current__].right)
		{
			if(puaMenu[__current__])
			{
				UWPMenu9 menu;
				POINT pt={rtList[__current__].left-5,-3};
				::ClientToScreen(_wndControl,&pt);
				menu.MapMenuFrom(puaMenu[__current__]);
				__in_menu_pop__ = TRUE;
				menu.TrackPopMenu(pt.x,pt.y,_wndControl,TPM_RIGHTALIGN);
				__in_menu_pop__ = FALSE;
			}
		}
		else
		{
			if(uaMessage[__current__])
			{
				::PostMessage(_wndControl,uaMessage[__current__],uwParam[__current__],ulParam[__current__]);
			}
		}
		break;
	}
	__current__ = -1;
}
